package br.com.crednove.paineis;

import br.com.crednove.dao.DAOCliente;
import br.com.crednove.modelo.Cliente;
import java.util.ArrayList;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;

public class PainelGraficosBarra extends javax.swing.JFrame {

    private DAOCliente daoCliente;
    private ArrayList<Cliente> listaClientes;

    public PainelGraficosBarra() {
        this.daoCliente = new DAOCliente();
        initComponents();
        setLocationRelativeTo(null);    
        String ano = this.jspAno.getValue().toString();
        this.criarGrafico(this.createDataset(ano), ano);
    }
    
    private void criarGrafico(CategoryDataset cds, String ano){
        String titulo = "Total de clientes por mês ("+ano+")";
        String eixoy = "Quantidade";
        String txt_legenda = "Ledenda:";
        boolean legenda = true;
        boolean tooltips = true;
        boolean urls = true;
        
        JFreeChart graf = ChartFactory.createBarChart3D(titulo, txt_legenda, eixoy, cds, PlotOrientation.VERTICAL, legenda, tooltips, urls);
        ChartPanel myChartPanel = new ChartPanel(graf, true);
        myChartPanel.setSize(jPanel1.getWidth(), jPanel1.getHeight());
        myChartPanel.setVisible(true);
        jPanel1.removeAll();
        jPanel1.add(myChartPanel);
        jPanel1.revalidate();
        jPanel1.repaint();
    }

    private CategoryDataset createDataset(String ano) {
        DefaultCategoryDataset dataset = new DefaultCategoryDataset();
        String sql = "", numMes = "", nomeMes = "";
        this.daoCliente.conectar();

        for (int i = 1; i <= 12; i++) {
            switch (i) {
                case 1: numMes = "01"; nomeMes = "Janeiro"; break;
                case 2: numMes = "02"; nomeMes = "Fevereiro"; break;
                case 3: numMes = "03"; nomeMes = "Março"; break;
                case 4: numMes = "04"; nomeMes = "Abril"; break;
                case 5: numMes = "05"; nomeMes = "Maio"; break;
                case 6: numMes = "06"; nomeMes = "Junho"; break;
                case 7: numMes = "07"; nomeMes = "Julho"; break;
                case 8: numMes = "08"; nomeMes = "Agosto"; break;
                case 9: numMes = "09"; nomeMes = "Setembro"; break;
                case 10: numMes = "10"; nomeMes = "Outubro"; break;
                case 11: numMes = "11"; nomeMes = "Novembro"; break;
                case 12: numMes = "12"; nomeMes = "Dezembro"; break;
            }
            sql = "SELECT CLIE_TRABALHADO FROM CLIENTE WHERE CLIE_INICIOPAG='"+ano+numMes+"01'";
            this.daoCliente.executaSQL(sql);
            dataset.addValue(this.daoCliente.getNumRegistros(), "Não Exportados", nomeMes);
            sql = "SELECT CLIE_TRABALHADO FROM CLIENTE WHERE CLIE_EXPORTADO = TRUE AND CLIE_INICIOPAG='"+ano+numMes+"01'";
            this.daoCliente.executaSQL(sql);
            dataset.addValue(this.daoCliente.getNumRegistros(), "Exportados", nomeMes);
            sql = "SELECT CLIE_TRABALHADO FROM CLIENTE WHERE CLIE_TRABALHADO = TRUE AND CLIE_INICIOPAG='"+ano+numMes+"01'";
            this.daoCliente.executaSQL(sql);
            dataset.addValue(this.daoCliente.getNumRegistros(), "Trabalhados", nomeMes);
        }
        this.daoCliente.desconectar();
        return dataset;
    }

    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();
        jspAno = new javax.swing.JSpinner();
        jLabel1 = new javax.swing.JLabel();
        btnOK = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Gráficos de Barra");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 269, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 835, Short.MAX_VALUE)
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 269, Short.MAX_VALUE)
        );

        jspAno.setModel(new javax.swing.SpinnerNumberModel(Integer.valueOf(2012), Integer.valueOf(2000), null, Integer.valueOf(1)));

        jLabel1.setText("Ano:");

        btnOK.setText("OK");
        btnOK.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnOKActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jspAno, javax.swing.GroupLayout.DEFAULT_SIZE, 85, Short.MAX_VALUE)
                    .addComponent(jLabel1)
                    .addComponent(btnOK, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jspAno, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnOK)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnOKActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnOKActionPerformed
        String ano = this.jspAno.getValue().toString();
        this.criarGrafico(this.createDataset(ano), ano);
    }//GEN-LAST:event_btnOKActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnOK;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JSpinner jspAno;
    // End of variables declaration//GEN-END:variables
}
